﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class ManageBooks : Form
    {
        public ManageBooks()
        {
            InitializeComponent();
        }
        string connectionString = Class1.connectionString;

        private void ManageBooks_Load(object sender, EventArgs e)
        {
            LoadBooks(); // 窗体加载时显示订单列表
        }
        private void dataGridViewBooks_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            LoadBooks();// 窗体加载时显示订单列表
        }
        private void LoadBooks()
        {
            string query = "SELECT * FROM Book";
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                dataGridViewBooks.DataSource = dt;
            }
        }

        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            string query = "INSERT INTO Book (BookID, Title, Course, Type, Stock) VALUES (@BookID, @Title, @Course, @Type, @Stock)";
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@BookID", txtBookID.Text);
                cmd.Parameters.AddWithValue("@Title", txtTitle.Text);
                cmd.Parameters.AddWithValue("@Course", txtCourse.Text);
                cmd.Parameters.AddWithValue("@Type", txtType.Text);
                cmd.Parameters.AddWithValue("@Stock", txtStock.Text);
                conn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("图书添加成功！");
                LoadBooks();
            }
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            string query = "DELETE FROM Book WHERE BookID = @BookID";
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@BookID", txtBookID.Text);
                conn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("图书删除成功！");
                LoadBooks();
            }
        }

        //private void btnModify_Click(object sender, EventArgs e)
        //{
        //    //string query = "INSERT INTO Book (BookID, Title, Course, Type, Stock) VALUES (@BookID, @Title, @Course, @Type, @Stock)";
        //    string query = @"UPDATE Book 
        //       SET Title = @Title, Course = @Course,
        //       Type = @Type, Stock = @Stock, CourseID = @CourseID
        //       WHERE BookID = @BookID";
        //    using (SqlConnection conn = new SqlConnection(connectionString))
        //    {
        //        SqlCommand cmd = new SqlCommand(query, conn);
        //        //cmd.Parameters.AddWithValue("@BookID", txtBookID.Text);
        //        cmd.Parameters.AddWithValue("@Title", txtTitle.Text);
        //        cmd.Parameters.AddWithValue("@Course", txtCourse.Text);
        //        cmd.Parameters.AddWithValue("@Type", txtType.Text);
        //        cmd.Parameters.AddWithValue("@Stock", txtStock.Text);
                               
        //        conn.Open();
        //        cmd.ExecuteNonQuery();
        //        MessageBox.Show("图书修改成功！");
        //        LoadBooks();
        //    }
        //}
    }
}
